package com.white.algebra.service.impl;



import com.white.algebra.dao.PermissionDao;
import com.white.algebra.dao.RoleDao;
import com.white.algebra.dao.impl.PermissionDaoImpl;
import com.white.algebra.dao.impl.RoleDaoImpl;
import com.white.algebra.domain.Permission;
import com.white.algebra.domain.Role;
import com.white.algebra.service.RoleService;
import java.sql.SQLException;
import java.util.List;



public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();
    private PermissionDao permissionDao = new PermissionDaoImpl();
    @Override
    public List<Role> findAll() throws SQLException {

        return roleDao.findAll();
    }

    @Override
    public void save(Role role) throws SQLException {
        roleDao.save(role);
    }

    @Override
    public List<Role> findById(String id) throws SQLException {
        List<Role> role= roleDao.findById(id);
        for (Role r:role
             ) {
            List<Permission> permission=permissionDao.findById(id);
            r.setPermissions(permission);
        }
        return role;
    }

    @Override
    public void deleteById(String roleId) throws SQLException {
        roleDao.deleteById(roleId);
    }

    @Override
    public List<Permission> findUserByIdAndAllRole(String id) throws SQLException {
        return permissionDao.findUserByIdAndAllRole(id);
    }

    @Override
    public void addpermission(String permissionId, String roleId) throws SQLException {
        permissionDao.addpermission(permissionId,roleId);
    }

    @Override
    public void deleteRole_PermissionById(String roleId) throws SQLException {
        roleDao.deleteRole_PermissionById(roleId);
    }

    @Override
    public void deleteUserAndRoleById(String roleId) throws SQLException {
        roleDao.deleteUserAndRoleById(roleId);
    }

    @Override
    public void deletePermissionById(String permissionId,String roleId) throws SQLException {
        permissionDao.deleteRole_PermissionByPermissionAndRoleId(permissionId,roleId);
    }

    @Override
    public List<Role> findRoleByPermissionId(String id) throws SQLException {
        return roleDao.findRoleByPermissionId(id);
    }

    @Override
    public List<Permission> findPermissionByRoleId(String id) throws SQLException {
        return permissionDao.findUserByIdAndAllRole(id);
    }


}
